package com.huw.cameramanagers;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.modifier.AlphaModifier;
import org.anddev.andengine.entity.modifier.LoopEntityModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import com.badlogic.gdx.math.Vector2;
import com.light.gamecomponents.IGameComponents;
import com.light.roller.ResourceManager;
import com.light.roller.Roller;

public class BlackGround implements IGameComponents{

	private Camera m_camera;
	private Sprite m_sprite;
	private String m_resourceName = "Black";
	private BaseGameActivity m_game;
	
	public void onMoveBlack()
	{
		m_sprite.setPosition(m_camera.getMinX()-100, m_camera.getMinY()-100);
	}
	
	public float getAlpha()
	{
		return m_sprite.getAlpha();
	}
	
	public BlackGround(Camera targetCamera,BaseGameActivity game)
	{
		m_camera = targetCamera;
		m_game = game;
	}
	
	@Override
	public void onLoadEngine() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onLoadResources() {
		ResourceManager.onLoadResources(m_resourceName, m_game);
		
	}

	@Override
	public void unLoadResources() {
		ResourceManager.unLoadResources(m_resourceName, m_game);
		
	}

	
	Scene m_scene;
	
	@Override
	public void onLoadScene(Scene scene, PhysicsWorld physicsWrold) {
		m_scene = scene;
		m_sprite = new Sprite(0,0,ResourceManager.getTextureRegion(m_resourceName));	
		m_sprite.setSize(Roller.CAMERA_WIDTH*3, Roller.CAMERA_HEIGHT*3);	
		m_sprite.setPosition(-Roller.CAMERA_WIDTH, -Roller.CAMERA_HEIGHT);
		m_sprite.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		m_sprite.setAlpha(1.0f);		
		m_scene.attachChild(m_sprite);
	}
	
	
	public void onLoadScene(Scene scene) {
		m_scene = scene;
		m_sprite = new Sprite(0,0,ResourceManager.getTextureRegion(m_resourceName));	
		m_sprite.setSize(Roller.CAMERA_WIDTH*3, Roller.CAMERA_HEIGHT*3);	
		m_sprite.setPosition(-Roller.CAMERA_WIDTH, -Roller.CAMERA_HEIGHT);
		m_sprite.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		m_sprite.setAlpha(1.0f);		
		m_scene.attachChild(m_sprite);
	}


	@Override
	public void removeFromScene(Scene scene, PhysicsWorld physicsWorld) {
		
		m_sprite = null;
		
	}

	@Override
	public void rotate(float angle) {
		// TODO Auto-generated method stub
		
	}

	public void setCenter(float x,float y)
	{
	    m_sprite.setPosition(x-m_sprite.getWidth()/2, y-m_sprite.getHeight()/2);
	}
	
	public Vector2 getCenter()
	{
		Vector2 temp = new Vector2();
		temp.x = m_sprite.getX()+m_sprite.getWidth()/2;
		temp.y = m_sprite.getY()+m_sprite.getHeight()/2;
		return temp;
	}
	
	LoopEntityModifier EntityModifier = null;
	
	public void LightOnCamera()
	{
		clearEntityModifier();
		EntityModifier = new LoopEntityModifier(0, new SequenceEntityModifier(new AlphaModifier(1f,1,0)));
		m_sprite.registerEntityModifier(EntityModifier);
	}
	
	public void LightOffCamera()
	{
		clearEntityModifier();
		m_scene.detachChild(m_sprite);
		m_scene.attachChild(m_sprite);
		EntityModifier = new LoopEntityModifier(0, new SequenceEntityModifier(new AlphaModifier(0.5f,0,1)));
		m_sprite.registerEntityModifier(EntityModifier);
	}
	
	public void clearEntityModifier()
	{
		if(EntityModifier!=null)m_sprite.unregisterEntityModifier(EntityModifier);
		EntityModifier = null;
	}

	public boolean isLightOn()
	{
		boolean result = false;
		if(m_sprite.getAlpha()==0.0f)result=true;
		return result;
	}
	
	public boolean isLightOff()
	{
		boolean result = false;
		if(m_sprite.getAlpha()==1.0f)result=true;
		return result;
	}

}
